package admin

import (
	"blogs/models"
	"blogs/utils"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
)

// Operations about object
type SettingController struct {
	beego.Controller
}

// @Title Get
// @Description 查询系统设置
// @Success 200 {object} models.Setting
// @router / [get]
func (o *SettingController) GetAll() {
	Setting := []models.Setting{}
	utils.DB.Find(&Setting)
	o.Data["json"] = map[string]interface{}{
		"msg":  "查询成功",
		"data": Setting[0],
		"code": false,
	}
	o.ServeJSON()
}

// @Title Update
// @Description 更新系统设置
// @Param	body		body 	models.Setting	true		"The body"
// @Success 200 {object} models.Setting
// @Failure 403 :objectId is empty
// @router /:objectId [put]
func (o *SettingController) Put() {

	// 接收参数
	var ob models.Setting
	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)

	// 更新数据
	Setting := models.Setting{}
	utils.DB.Where("id=?", 1).Find(&Setting)

	Setting.Notice = ob.Notice
	Setting.Label = ob.Label
	Setting.Article = ob.Article
	Setting.Classification = ob.Classification
	Setting.LastUpdate = ob.LastUpdate
	Setting.Page = ob.Page
	Setting.Comment = ob.Comment
	utils.DB.Save(&Setting)
	o.Data["json"] = map[string]interface{}{
		"msg":  "更新成功",
		"data": "更新成功",
		"code": true,
	}
	o.ServeJSON()
}
